﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Service;
using LIMS.Entities;
using Castle.Core.Logging;

namespace LIMS.Shared
{
    public class GroupManagementPresenter : BasePresenter<IGroupManagementView>
    {
        private IGroupManagementView view;
        private IGroupManagementService  groupService;
        private IUserManagementService userService;
        private ILogger logger;
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="view"></param>
        /// <param name="cService"></param>
        /// <param name="cUserService"></param>
        /// <param name="oLogger"></param>
        public GroupManagementPresenter(IGroupManagementView view, 
                                        IGroupManagementService cService,
                                        IUserManagementService cUserService,
                                        ILogger oLogger)
            : base(view)
        {
            this.view = view;
            this.groupService = cService;
            this.userService = cUserService;
            this.logger = oLogger;
        }

        public GroupManagementPresenter(IGroupManagementView view, ILogger oLogger
                                        )
            : base(view)
        {
            this.view = view;
           // this.groupService = cService;
            //this.userService = cUserService;
            this.logger = oLogger;
            
        }
        /// <summary>
        /// Initialize View for GroupManagementPresenter
        /// </summary>
        /// <returns></returns>
        public bool InitializeView()
        {
            try
            {
                logger.Info("Initializing group management view");
                view.oGroupList = groupService.GetAllGroups();
                view.oUsers = groupService.GetAllUsers();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error while initializing view. Error : " + ex.Message, ex);
                return false;
            }

        }

        public string ShowMessage()
        {
            List<User> oUser = new List<User>();
            oUser = userService.GetAllUsers();
            return "Ank";
        }

        /// <summary>
        /// Saves group
        /// </summary>
        /// <param name="oGroup"></param>
        /// <returns></returns>
        public bool Save(Group oGroup)
        {
            return groupService.Save(oGroup);
        }

        /// <summary>
        /// Deletes group
        /// </summary>
        /// <param name="oGroup"></param>
        /// <returns></returns>
        public bool Delete(Group oGroup)
        {
            // mark this group as deactive
            oGroup.Status = "D";
            return groupService.Save(oGroup);
        }
        /// <summary>
        /// Edit Group
        /// </summary>
        /// <param name="index"></param>
        public bool Edit(int index)
        {
            try
            {
                view.CurrentIndex = index;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error occured while editing record. Exception : " + ex.Message, ex);
                return false;
            }
        }

        /// <summary>
        /// Get All User List
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllUserList()
        {
           List<string> userList = new List<string>();
           try
           {
               foreach (User oUser in userService.GetAllUsers())
                   userList.Add(oUser.UserName + "[" + oUser.LastName + " , " + oUser.FirstName + "]");
               return userList;
           }
           catch (Exception ex)
           {
               logger.Error("Error occured while retrieving all users.",ex); 
               return userList;
           }
        }

        /// <summary>
        /// Checks if a group name is duplicate in database
        /// </summary>
        /// <param name="sUserName"></param>
        /// <returns></returns>
        public bool IsDuplicateGroupName(string sGrouprName)
        {
             //Check if we have existing user, new user will have Id = 0
            if(view.oGroupList[view.CurrentIndex].Id > 0)
            {
                //Check if user has changed user name, if not, don't check for duplicate.
                if (view.oGroupList[view.CurrentIndex].Name.ToUpper().Trim() == sGrouprName.ToUpper().Trim())
                    return false;
            }
            //In rest of the cases perform normal check
            if (groupService.GetGroupIdByGroupName(sGrouprName) != 0)
                return true;
            else
                return false;
       }
        
        /// <summary>
        /// Get user by database Id
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public string GetUserByUserId(int Id)
        {
            try
            {
                User oUser = userService.GetUser(Id);
                return oUser.UserName + "[" + oUser.LastName + " , " + oUser.FirstName + "]";
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting user. Error: " + ex.Message, ex);
                return string.Empty;
            }
        }
        /// <summary>
        /// Get User Database Id By UserName
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public int GetUserDBIdByUserName(string username)
        {
            string[] sThisUserName = username.Split('[');
            username = sThisUserName[0];
           User oUser =  userService.GetUserByUserID(username);
           return oUser.Id;
        }
        public List<Group> SearchGroupByName(string sGroupName)
        {
            return groupService.SearchGroupByName(sGroupName) ;
        }
    
    }
}
